package com.whale.springsession.service;

import com.whale.springsession.exception.MyAuthenticationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

/**
 * @author raven
 * @since 2021/9/1
 */
@Component("ucFormLoginAuthenticationProvider")
public class UcFormLoginAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {

    @Autowired
    MyUserServiceImpl myUserService;

    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication)
        throws AuthenticationException {
        if (authentication.getPrincipal().equals("admin")) {
            throw new MyAuthenticationException("admin找不到");
        }

        System.out.println("账号密码验证通过");
    }

    @Override
    protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
        return myUserService.loadUserByUsername(username);
    }


}
